package com.gaozhaoyang.dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.SQLException;

/*
 *  增删改 无事务的操作
 *      new QueryRunner() 核心对象
 *          int update()增删改
 * */
public class Dbutils_Demo01 {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //创建可执行对象

    QueryRunner qr = new QueryRunner(dataSource);
    @Test
    public void  addUser() throws SQLException {
        String sql ="insert into user values(?,?,?)";
        Object arr[] ={1, "sunwukong", "123"};
        System.out.println(qr.update(sql,arr) > 0 ? "添加成功" : "添加失败");
    }
    @Test
    public void updateUser() throws SQLException {
        String sql = "update user set username=? where uid =?";
        System.out.println(qr.update(sql, "niumowang", 1) > 0 ? "修改成功" : "修改失败");
    }
    @Test
    public void deleteUser() throws SQLException {
        String sql = "delete from user where uid =?";
        int row = qr.update(sql, 1);
        if (row>0) {
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
}
